const Bmob = require('../../utils/bmob')
const utils = require('../../utils/utils')
const _ = require('../../utils/underscore')
Page({
  data: {
    payload: [],
    pageIndex: 0,
    currencies: [
      'btc',
      'eos',
      'ltc',
      'xrp',
      'eth',
      'bsv',
      'etc',
      'bch'
    ],
    items: [],
    currency: 'btc'
  },
  radioChange: function (e) {
    let currency = e.detail.value
    this.setData({
      currency: currency,
      currencies: [currency]
    });
  },
  onLoad() {
    this.loadData()
    let items = this.data.currencies.map(item => {
      return {
        value: item,
        title: item,
        checked: false
      }
    })
    this.setData({
      items: items
    })
  },
  loadData() {
    let query = new Bmob.Query('Future')
    query.descending('createdAt');
    query.skip(this.data.pageIndex * utils.pageSize)
    query.limit(utils.pageSize)
    query.find().then(results => {
      wx.stopPullDownRefresh();
      results.map(item => {
        item.set('createdAt', utils.todayDateTime(item.createdAt))
        return item
      })
      results = utils.formatResults(results)

      let payload = this.data.payload
      if (this.data.pageIndex == 0) {
        payload = []
      }
      payload = payload.concat(results)
      // 计算平均数，最小值，最大值
      let items = this.data.items
      items.map(item => {
        let currency = item.value
        let currencyArray = payload.map(item => {
          return item[currency]
        })
        const max = _.max(currencyArray, function (value) {
          return value.rate
        })
        const min = _.min(currencyArray, function (value) {
          return value.rate
        })
        const maxEnter = _.max(currencyArray, function (value) {
          return value.rateEnter
        })
        const minEnter = _.min(currencyArray, function (value) {
          return value.rateEnter
        })
        const sum = _.reduce(currencyArray.map(item => item.rate), function (memo, num) {
          return memo + num
        }, 0)
        const sumEnter = _.reduce(currencyArray.map(item => item.rateEnter), function (memo, num) {
          return memo + num
        }, 0)
        item.max = max.rate
        item.min = min.rate
        item.maxEnter = maxEnter.rateEnter
        item.minEnter = minEnter.rateEnter
        item.average = (sum / currencyArray.length).toFixed(2)
        item.averageEnter = (sumEnter / currencyArray.length).toFixed(2)
        item.margin = (max.rate - min.rate).toFixed(2)
        item.marginEnter = (maxEnter.rateEnter - minEnter.rateEnter).toFixed(2)
        return item
      })
      this.setData({
        payload: payload,
        items: items,
        loadingTip: results.length < utils.pageSize ? '已经没有更多啦' : '上拉加载更多'
      })
    })
  },
  onPullDownRefresh: function () {
    // 下拉刷新
    this.setData({
      pageIndex: 0
    })
    this.loadData();
  },
  onReachBottom() {
    let pageIndex = this.data.pageIndex
    pageIndex++
    this.setData({
      pageIndex: pageIndex
    })
    this.loadData()
  },
  onShareAppMessage: function () {
    return {
      title: 'USDT价格',
      desc: '来自OKEx法币数据',
    }
  },
})